package com.harwkin.nb.camera;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.harwkin.nb.camera.model.PicsItem;
import com.harwkin.nb.camera.model.PicsItemList;
import com.lidroid.xutils.util.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.BaseFragmentActivity;
import cn.afactory.tygz.ui.base.utils.DialogUtil;

/**
 * 查看大图的页面
 */
public class PageBigImageActivity extends BaseFragmentActivity {
    public static String IMAGE_LIST_DATA = "IMAGE_LIST_DATA";
    public static String SELECT_LIST_POSITION = "SELECT_LIST_POSITION";
    public static String SHOW_DELETE_BTN = "SHOW_DELETE_BTN";
    public static String NEED_RESULT = "NEED_RESULT";
    public static final int REQ_CHOOSE_MAP = 0x10;
    ViewPager mVpImages;
    ImageViewerPagerAdapter mAdapter;
    private int mSelectPosition;
    private ArrayList<PicsItem> mData;
    private boolean mNeedResult = false;

    public static Intent getIntent(Context context, ArrayList<PicsItem> data, int selectPosition, boolean canDel, boolean needResult) {
        Intent intent = new Intent(context, PageBigImageActivity.class);
        intent.putExtra(IMAGE_LIST_DATA, data);
        intent.putExtra(SELECT_LIST_POSITION, selectPosition);
        intent.putExtra(SHOW_DELETE_BTN, canDel);
        intent.putExtra(NEED_RESULT, needResult);
        return intent;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_page_big_image);

//        setTitleText(getString(R.string.label_picture_big_image_name));
        mData = (ArrayList<PicsItem>) getIntent().getSerializableExtra(IMAGE_LIST_DATA);
        initViews();
        initData();

    }

    private void initData() {
        mSelectPosition = getIntent().getIntExtra(SELECT_LIST_POSITION, 0);
        mNeedResult = getIntent().getBooleanExtra(NEED_RESULT, false);
        if (mData != null) {
            if (mAdapter.getCount() > mSelectPosition) {
                mVpImages.setCurrentItem(mSelectPosition);
            }
            mAdapter.notifyDataSetChanged();
            if (getIntent().getBooleanExtra(SHOW_DELETE_BTN, false)) {
                setRightButton("删除",new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDelPicDialog();
                    }
                });
            } else {
//                setRightImageView(R.drawable.ic_menu_more, new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (TimeUtil.isFastDoubleClick()) {
//                            return;
//                        }
//                        try {
//                            mAdapter.getMap().get(mData.get(mVpImages.getCurrentItem())).PopView(v);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
            }
        }
        mVpImages.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                refreshTitle(i + 1);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        refreshTitle(mSelectPosition + 1);
    }
    Dialog msgDlg = null;
    private void showDelPicDialog(){
        msgDlg = DialogUtil.showMessageDialog(this,
                null,
                getString(R.string.dlg_msg_delete_pic),
                getString(R.string.label_btn_delete),
                getString(R.string.label_btn_cancel),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(msgDlg != null) {
                            msgDlg.dismiss();
                        }
                        delPictures();
                    }
                },
                null);
    }
    //删除图片
    private void delPictures(){
        delData();
    }

    private void delData() {
        int pos = mVpImages.getCurrentItem();
        mData.remove(pos);
        if (mData.size() <= 0) {
            finish();
        }
//        mAdapter.notifyDataSetChanged();
        mAdapter = new ImageViewerPagerAdapter(getSupportFragmentManager());
        mVpImages.setAdapter(mAdapter);
        mVpImages.setCurrentItem(pos - 1 >= 0 ? pos - 1 : 0);
        LogUtils.e("loza: " + mVpImages.getCurrentItem());
        refreshTitle(pos - 1 >= 0 ? pos : 1);
    }

    /**
     * 刷新标题栏
     *
     * @param index
     */
    private void refreshTitle(int index) {
        setTitleText(index + "/" + mData.size());
    }

    private void initViews() {
        mVpImages = (ViewPager) findViewById(R.id.vp_images);
        mAdapter = new ImageViewerPagerAdapter(getSupportFragmentManager());
        mVpImages.setAdapter(mAdapter);
    }


    class ImageViewerPagerAdapter extends FragmentStatePagerAdapter {
        Map<String, PageBigImageFragment> map = new HashMap<String, PageBigImageFragment>();

        public ImageViewerPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
//            if(i >= mlist.size()){
//                mlist.add(i,PageBigImageFragment.getPageBigImageFragment(mData.get(i)));
//            }
            map.put(mData.get(i).image,PageBigImageFragment.getPageBigImageFragment(mData.get(i)));
            return map.get(mData.get(i).image);
        }

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

        public Map<String, PageBigImageFragment> getMap(){
            return map;
        }
    }

    @Override
    public void finish() {
        if (mNeedResult) {
            Intent extrasIntent = new Intent();
            if (mData != null) {
                PicsItemList picsItemList = new PicsItemList();
                picsItemList.list = mData;
                extrasIntent.putExtra(IMAGE_LIST_DATA, picsItemList);
            }
            setResult(Activity.RESULT_OK, extrasIntent);
        }
        super.finish();
    }
}
